package com.juai.centralkitchen.activity;

import android.content.Intent;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.bridge.common.base.BaseActivity;
import com.bridge.common.bean.Event;
import com.bridge.common.bean.EventCode;
import com.bridge.common.bean.Loginbean;
import com.bridge.common.bean.MessageBean;
import com.bridge.common.body.LoginBody;
import com.bridge.common.body.MessageBody;
import com.bridge.common.dialog.VDialog;
import com.bridge.common.http.RetrofitHelper;
import com.bridge.common.http.ToastUtil;
import com.bridge.common.network.HttpDisposable;
import com.bridge.common.utils.ActivityUtil;
import com.bridge.common.utils.SpUtils;
import com.bridge.common.weight.MultipleStatusView;
import com.juai.centralkitchen.R;
import com.juai.centralkitchen.adapter.MessageListAdapter;
import com.juai.centralkitchen.databinding.ActivityMessageBinding;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ClassName SetttingActivity
 * @Description TODO 消息列表
 * @Author biekangdong
 * @CreateDate 2021/5/13 14:53
 * @Version 1.0
 * @UpdateDate 2021/5/13 14:53
 * @UpdateRemark 更新说明
 *
 */
public class MessageActivity extends BaseActivity {
    private ActivityMessageBinding binding;

    private MessageListAdapter messageListAdapter;
    private List<MessageBean.DataBean.ListBean> messageList = new ArrayList<>();
    private MessageBody messageBody;
    private int pageNum=1;
    private boolean isLayoutRefresh = true;
    @Override
    protected int layoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void initView() {
        binding=getBind();

        initRefresh();
    }

    @Override
    protected void setListenner() {

    }

    private void initRefresh() {
        binding.layMultiLayout.setStrEmpty("暂无消息~");
        binding.layMultiLayout.setIconEmptyResources(R.mipmap.view_statues_empty);
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        binding.refreshLayout.setFooterHeight(50);//Footer标准高度（显示上拉高度>=标准高度 触发加载）
        binding.refreshLayout.setEnableFooterFollowWhenLoadFinished(true);
        binding.refreshLayout.setEnableLoadMore(true);
        binding.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                isLayoutRefresh = true;
                refreshLayout.setNoMoreData(false);
                pageNum = 1;
                httpSelectOrderList();
            }

            @Override
            public void onLoadMore(final RefreshLayout refreshLayout) {
                isLayoutRefresh = false;
                pageNum++;
                httpSelectOrderList();
            }
        });

        initRclAdapter();
        httpSelectOrderList();
    }



    //刷新成功
    private void refreshSuccess() {
        if (pageNum == 1) {
            binding.refreshLayout.finishRefresh(true);//传入false表示刷新失败
            binding.layMultiLayout.showContent();
            messageList.clear();
        } else {
            binding.refreshLayout.finishLoadMore(true);
        }
    }


    //刷新无数据
    private void refreshNoData() {
        if (pageNum == 1) {
            binding.refreshLayout.finishRefresh(false);//传入false表示刷新失败
            binding.layMultiLayout.showEmpty();
        } else {
            binding.refreshLayout.finishLoadMoreWithNoMoreData();
        }

    }

    //刷新失败
    private void refreshError() {
        if (pageNum == 1) {
            binding.refreshLayout.finishRefresh(false);//传入false表示刷新失败
            binding.layMultiLayout.showError();
        } else {
            binding.refreshLayout.finishLoadMore(false);
        }
    }

    public void initRclAdapter(){
        messageListAdapter = new MessageListAdapter(mContext, R.layout.item_message_list, messageList);
        binding.rclView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.rclView.setAdapter(messageListAdapter);
    }

    public void httpSelectOrderList(){
        if(messageBody==null){
            messageBody=new MessageBody();
        }
        messageBody.setPageSize(10);
        messageBody.setPageNum(pageNum);
        RetrofitHelper.getApiService().selectOrderList(messageBody)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpDisposable<MessageBean>() {
                    @Override
                    protected void onStart() {
                        super.onStart();
                        if (isLayoutRefresh) {
                            binding.layMultiLayout.showLoading();
                        }

                    }

                    @Override
                    public void success(MessageBean dataBean) {
                        refreshSuccess();
                        if (dataBean.getCode() == 200) {
                            List<MessageBean.DataBean.ListBean> dataList = dataBean.getData().getList();
                            if (dataList.size() > 0) {
                                messageList.addAll(dataList);
                                messageListAdapter.setData(messageList);
                                messageListAdapter.notifyDataSetChanged();


                            } else {
                                refreshNoData();
                            }
                        }else if (dataBean.getCode() == 401){
                            ToastUtil.showToast(mContext, dataBean.getMsg());
                            SpUtils.putData(mContext,SpUtils.ISLOGIN,0);
                            SpUtils.putData(mContext,SpUtils.TOKEN,"");
                            ActivityUtil.finishAllActivity();
                            startActivity(new Intent(mContext,LoginActivity.class));
                        }else {
                            refreshError();
                        }

                    }

                    @Override
                    public void onError(String msg) {
                        refreshError();
                    }
                });

    }


    @Override
    public void onEventBusCome(Event event) {
        super.onEventBusCome(event);
        switch (event.getCode()) {
            case EventCode.EVENT_CODE_REFRESHMESSAGE:
                isLayoutRefresh = true;
                binding.refreshLayout.setNoMoreData(false);
                pageNum = 1;
                httpSelectOrderList();
                break;
        }
    }

}
